package cn.com.dhcc.turbo.entity.sysmgr.taskmont;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.springframework.jdbc.core.RowMapper;

import cn.com.dhcc.app.core.handler.dbhandler.WappedResultSet;
import cn.com.dhcc.app.core.util.DateUtil;

/**
 * <b>任务监控RowMapper类</b>
 * @author Macky_He
 * @version [数据交换平台V1.2.1]
 */
public class TriggerRowWapper implements RowMapper<QuartzTriggerVO> {

	/**
	 * <b>从数据库查询的结果集中获取数据</b>
	 */
	@Override
	public QuartzTriggerVO mapRow(ResultSet rs, int rowNum) throws SQLException {
		WappedResultSet rss = new WappedResultSet(rs);
		QuartzTriggerVO vo = new QuartzTriggerVO();
		vo.setJobName(rss.getString("jobName"));
		vo.setTriggerName(rss.getString("triggerName"));
		vo.setPrevTime(format(rss.getString("prevTime"), DateUtil.FMT_yyyymmddHHmmss));
		vo.setNexTime(format(rss.getString("nexTime"), DateUtil.FMT_yyyymmddHHmmss));
		vo.setStartTime(format(rss.getString("startTime"), DateUtil.FMT_yyyymmddHHmmss));
		vo.setState(rss.getString("state"));
		vo.setCronExpression(rss.getString("cronExpression"));
		return vo;
	}
	
	/**
	 * 格式化日期
	 * @param time UTC时间
	 * @param parrent 格式化样式
	 * @return 格式化后的日期
	 */
	public static String format(String time, String parrent) {
		Calendar ca = Calendar.getInstance();
		//如果日期为-1代表未执行过任务，返回空
		if ("-1".equals(time)){
			return "";
		}
		try {
			ca.setTimeInMillis(Long.parseLong(time));
		} catch (NumberFormatException e) {
			return "";
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat(parrent);
		return sdf.format(ca.getTime());
	}

}
